package com.s59.rabc.controller;

import com.s59.rabc.commen.Dta;
import com.s59.rabc.commen.RequiredPerms;
import com.s59.rabc.entity.Role;
import com.s59.rabc.service.IRoleService;
import lombok.RequiredArgsConstructor;
//import org.apache.shiro.authz.annotation.RequiresPermissions;
//import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 角色信息表 前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2023-05-15
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IRoleService roleService;

    @PostMapping
//    @RequiredPerms("role:add")
//    @RequiresPermissions("role:add")
    @PreAuthorize("hasAnyAuthority('role:add')")
    public Dta add(@RequestBody Role role){
        roleService.save(role);
        return Dta.ok();
    }

//    @RequiresRoles("manager")
    @DeleteMapping("/{id}")
//    @RequiredPerms("role:delete")
    @PreAuthorize("hasAnyRole('role:delete')")
    public Dta del(@PathVariable Long id){
        roleService.removeById(id);
        return Dta.ok();
    }

    @PutMapping
    @PreAuthorize("hasAnyAuthority('role:update')")
//    @RequiresPermissions("role:update")
//    @RequiredPerms("role:update")
    public Dta update(@RequestBody Role role){
        roleService.saveOrUpdate(role);
        return Dta.ok();
    }

    @GetMapping
    @PreAuthorize("hasAnyAuthority('role:list')")
//    @RequiresPermissions("role:list")
//    @RequiredPerms("role:list")
    public Dta getAll(){
        List<Role> list =roleService.list();
        return Dta.ok(list);
    }
}
